﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Alma.Models;
using AlmaDatabase;
using System.Diagnostics;

namespace Alma.Controllers
{
    public class SectionController : Controller
    {
        //
        // GET: /Section/Details/5

        public ActionResult Details(int id)
        {
            using (var context = new AlmaDBEntities())
            {
                var section = from u in context.Sections
                              where u.ID == id
                              select u;
                return View(section.First());
            }
        }

        public ActionResult Show(int id)
        {
            int mysection = id;
            ProductListModel productModel = new ProductListModel();
            productModel.List = new List<ProductDetailsModel>();
            productModel.ID = id;
            using (var entities = new AlmaDBEntities())
            {
                foreach (var product in (from p in entities.Products where p.ID_Section == mysection select p))
                {
                    ProductDetailsModel pro = new ProductDetailsModel();
                    pro.Name = product.Name;
                    pro.ID = product.ID;
                    pro.Description = product.Description;
                    if (product.ImgUrl == null)
                        pro.ImgUrl = "~/Content/images/unavailable.gif";
                    else
                        pro.ImgUrl = product.ImgUrl;
                    productModel.List.Add(pro);
                }
                var section = (from p in entities.Sections where p.ID == id select p).First();
                productModel.Name = section.Name;

                var cat = (from p in entities.Categories where p.ID == section.ID_Category select p).First();
                productModel.ID_Cat = cat.ID;
                productModel.Name_Cat = cat.Name;

                productModel.Criterions = entities.SectionCriterions
                    .Include("Criterion")
                    .Where(x => x.ID_Section == section.ID).OrderBy(x => x.ViewOrder)
                    .ToList();


            }
            return View(productModel);
        }

        public ActionResult Search(int id)
        {
            var query = "";
            using (var entities = new AlmaDBEntities())
            {
                foreach (string critKey in Request.Form.Keys)
                {
                    if (!critKey.StartsWith("Criterion_")) continue;

                    var critValue = Request.Form[critKey];
                    if (critValue == "") continue;

                    var critID = int.Parse(critKey.Substring("Criterion_".Length));

                    var crit = entities.Criteria.Where(x => x.ID == critID).Single();

                    query += " @" + crit.Name + (crit.Method == 0 ? "#" : "=") + critValue;
                }

                var secName = from x in entities.Sections.Where(x => x.ID == id) select new { x.Name };
                query += " sec:\"" + secName.Single().Name + "\"";

            }
            
            query = query.Trim();
            return RedirectToAction("Index", "Search", new {s = query});
        }
    }
}